﻿using System.Net;
using DemoWebapi01.Entities;
using MailKit.Net.Smtp;
using MimeKit;

namespace DemoWebapi01.MyServices
{
    public class EmailService : IEmailService
    {
        private readonly IConfiguration _configuration;

        public EmailService(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public async Task SendVerificationEmailAsync(string userId, string email, string verificationCode)
        {
            var domain = _configuration["Yourdomain"];
            var verificationLink = $"{domain}/api/User/VerifyEmail/verify/{userId}/{verificationCode}"; // 生成验证链接

            var smtpSettings = _configuration.GetSection("SmtpSettings");
            var smtpServer = smtpSettings["Server"];
            var port = int.Parse(smtpSettings["Port"]);
            var username = smtpSettings["Username"];
            var password = smtpSettings["Password"];
            try
            {
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress("TestName", username));
                message.To.Add(new MailboxAddress("Recipient Name", email));
                message.Subject = "请验证您的邮箱";

                var messageText = $"点击以下链接激活您的账号" + Environment.NewLine + $"<a>{verificationLink}</a>" + Environment.NewLine + "10分钟内有效。";
                // 创建邮件内容
                var bodyBuilder = new BodyBuilder
                {
                    HtmlBody = @"
                        <html>
                            <head>
                                <style>
                                    body { font-family: Arial, sans-serif; }
                                    a { color: #007BFF; text-decoration: none; }
                                    a:hover { text-decoration: underline; }
                                </style>
                            </head>
                            <body>
                                <h1>Welcome to Our Website!</h1>
                                <p>Thank you for signing up. Click the link below to verify your email address:</p>
                                <p><a href='"+ verificationLink + @"'>Verify Email Address</a></p>
                                <p>If the link does not work, copy and paste this URL into your browser:</p>
                                <p style='font-weight: bold'>" + verificationLink + @"</p>
                                <p>The link is valid for 10 minutes.</p>
                                <p>Best regards</p>
                            </body>
                        </html>
                    "
                };

                // 将邮件内容添加到消息对象中
                message.Body = bodyBuilder.ToMessageBody();

                using (var client = new SmtpClient())
                {
                    await client.ConnectAsync("smtp.qq.com", 465, true);
                    await client.AuthenticateAsync(username, password);
                    await client.SendAsync(message);
                    await client.DisconnectAsync(true);
                }
                Console.WriteLine("邮件发送成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"邮件发送失败: {ex.Message}");
                throw;
            }
        }
    }
}
